package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.UserTaskRecordDAO;
import com.daxia.ogmall.dto.UserTaskRecordDTO;
import com.daxia.ogmall.model.UserTaskRecord;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class UserTaskRecordService {
	
	@Autowired
	private UserTaskRecordDAO userTaskRecordDAO;
	
	public List<UserTaskRecordDTO> find(UserTaskRecordDTO query, Page page) {
		List<UserTaskRecord> models = userTaskRecordDAO.find(query, page);
		List<UserTaskRecordDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<UserTaskRecordDTO> findAll() {
		return this.find(new UserTaskRecordDTO(), null);
	}
	
	public List<UserTaskRecordDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new UserTaskRecordDTO(), page);
	}
	
	public Long create(UserTaskRecordDTO dto) {
		UserTaskRecord model = new UserTaskRecord();
		toModel(model, dto);
		return userTaskRecordDAO.create(model);
	}
	
	public UserTaskRecordDTO load(Long id) {
	    UserTaskRecord model = userTaskRecordDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(UserTaskRecordDTO dto) {
		UserTaskRecord model = userTaskRecordDAO.load(dto.getId());
		toModel(model, dto);
		userTaskRecordDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				userTaskRecordDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public UserTaskRecordDTO findOne(UserTaskRecordDTO query) {
		UserTaskRecord model = userTaskRecordDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<UserTaskRecordDTO> toDTOs(List<UserTaskRecord> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<UserTaskRecordDTO>(0);
		}
		List<UserTaskRecordDTO> dtos = new ArrayList<UserTaskRecordDTO>(models.size());
		for (UserTaskRecord model : models) {
	        UserTaskRecordDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private UserTaskRecordDTO toDTO(UserTaskRecord model) {
		if (model == null) {
			return null;
		}
		UserTaskRecordDTO dto = BeanMapper.map(model, UserTaskRecordDTO.class);
		
		return dto;
	}
	
	private void toModel(UserTaskRecord model, UserTaskRecordDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<UserTaskRecord> toModels(List<UserTaskRecordDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<UserTaskRecord>(0);
		}
		List<UserTaskRecord> models = new ArrayList<UserTaskRecord>(dtos.size());
		for (UserTaskRecordDTO dto : dtos) {
	        UserTaskRecord model = new UserTaskRecord();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
